package com.paireach.szwolaiyun.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.fragmentall.DcFinishDischargeCargoFragment;
import com.paireach.szwolaiyun.fragmentall.DcFinishGoodsReceiptFragment;
import com.paireach.szwolaiyun.fragmentall.DcSignFragment;
import com.paireach.szwolaiyun.fragmentall.DcStartDischargeCargoFragment;
import com.paireach.szwolaiyun.fragmentall.FinishDischargeCargoFragment;
import com.paireach.szwolaiyun.fragmentall.FinishGoodsReceiptFragment;
import com.paireach.szwolaiyun.fragmentall.FinishLoadingFragment;
import com.paireach.szwolaiyun.fragmentall.GoodsReceiptFactorySignFragment;
import com.paireach.szwolaiyun.fragmentall.StartDischargeCargoFragment;
import com.paireach.szwolaiyun.fragmentall.StartLoadingFragment;
import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * @deprecated Use Activity_X instead
 */
@Deprecated
public class ContainerActivity extends BaseActivity {

    private String extra;
    private DriveOrder driveOrder;
    private int x;
    private String orderNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        driveOrder = (DriveOrder) getIntent().getSerializableExtra("DriveOrder");
        x = getIntent().getIntExtra("fragments", 0);
        orderNumber = getIntent().getStringExtra("orderNumber");
        if (TextUtils.isEmpty(orderNumber))
            loadFragments(x);
        else
        //do request and setup driverOrder object
        {
            showProgressDialog();
            new WayBillRequest().getOrderDetailByOrderNumberAndPhone(orderNumber,
                    SharedPreferencesUtil.getValue(this, SharedPreferencesUtil.USER_PHONE));
        }

    }

    private void loadFragments(int x) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (x) {
            case 228:
                //装货中
                StartLoadingFragment loadingFragment = StartLoadingFragment.getInstance(driveOrder.getOrderNo());
                transaction.replace(R.id.frame, loadingFragment, StartLoadingFragment.TAG).commit();
                break;
            case 230:
                //装货完成正在出厂
                FinishLoadingFragment fragment5 = FinishLoadingFragment.getInstance(driveOrder.getOrderNo());
                transaction.replace(R.id.frame, fragment5, FinishLoadingFragment.TAG).commit();
                break;
            case 232:
                //送货在途,是可以进行签到的状态
                if (driveOrder.getDcDeptType().equals("dc")) {
                    //dc 签到
                    DcSignFragment fragment1 = DcSignFragment.getInstance(driveOrder);
                    transaction.replace(R.id.frame, fragment1, DcSignFragment.TAG).commit();
                } else if (driveOrder.getDcDeptType().equals("factory")) {
                    //工厂签到
                    GoodsReceiptFactorySignFragment fragment1 = GoodsReceiptFactorySignFragment.getInstance(driveOrder);
                    transaction.replace(R.id.frame, fragment1, GoodsReceiptFactorySignFragment.TAG).commit();
                }
                break;
            //以下卸货方式需要判断是哪一种卸货方式：工厂还是dc还是个人
            case 240:
                //收货中
                if (driveOrder.getDcDeptType().equals("dc")) {
                    DcStartDischargeCargoFragment fragment1 = DcStartDischargeCargoFragment.getInstance(driveOrder.getOrderNo());
                    transaction.replace(R.id.frame, fragment1, DcStartDischargeCargoFragment.TAG).commit();
                } else if (driveOrder.getDcDeptType().equals("factory")) {
                    StartDischargeCargoFragment fragment6 = StartDischargeCargoFragment.getInstance(driveOrder.getOrderNo());
                    transaction.replace(R.id.frame, fragment6, StartDischargeCargoFragment.TAG).commit();
                }
                break;
            case 242:
                //离厂(收)中
                if (driveOrder.getDcDeptType().equals("dc")) {
                    DcFinishDischargeCargoFragment fragment1 = DcFinishDischargeCargoFragment.getInstance(driveOrder.getOrderNo());
                    transaction.replace(R.id.frame, fragment1, DcFinishDischargeCargoFragment.TAG).commit();
                } else if (driveOrder.getDcDeptType().equals("factory")) {
                    FinishDischargeCargoFragment fragment3 = FinishDischargeCargoFragment.getInstance(driveOrder.getOrderNo());
                    transaction.replace(R.id.frame, fragment3, FinishDischargeCargoFragment.TAG).commit();
                }
                break;
            case 244:
            case 248:
                if (driveOrder.getDcDeptType().equals("dc")) {
                    DcFinishGoodsReceiptFragment fragment1 = new DcFinishGoodsReceiptFragment();
                    transaction.replace(R.id.frame, fragment1, DcFinishGoodsReceiptFragment.TAG).commit();
                } else if (driveOrder.getDcDeptType().equals("factory")) {
                    FinishGoodsReceiptFragment fragment3 = new FinishGoodsReceiptFragment();
                    transaction.replace(R.id.frame, fragment3, FinishGoodsReceiptFragment.TAG).commit();
                }
                break;
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("get_order_detail_info")) {
            driveOrder = (DriveOrder) event.getObj1();
            loadFragments(driveOrder.getOrderStateCode());
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.CAN_LOAD_ENTER ||
                event.getRequestTag() == RequestTag.CAN_RECEIPT_ENTER ||
                event.getRequestTag() == RequestTag.GET_ORDER_DETAIL) {
            dismissProgressDialog();
            showToast("数据获取失败！");
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.GET_ORDER_DETAIL) {
            dismissProgressDialog();
            JsonPaser.parseDriverOrderDetail(event.getJson());
        }
    }

}
